package com.coder4j.starter.mybatis.provider;

import com.coder4j.starter.mybatis.SqlFieldReader;
import com.coder4j.starter.tools.util.Console;
import com.coder4j.starter.tools.util.ReflectUtils;
import com.coder4j.starter.tools.util.StringUtils;

import java.util.List;

/**
 * 基础的Update语句提供类
 * @author Keller
 * @date 2019-07-18 14:27:30
 */
public class BaseUpdateProvider {

    /**
     * 根据id 更新数据
     * @param <T>
     * @return UPDATE router SET methodName = #{methodName} ,createTime = #{createTime} WHERE id = #{id}
     */
    public static <T> String updateById(ProviderVO<T> providerVO){
        String tableName = providerVO.getTableName();
        T entity = providerVO.getEntity();
        String sql = getUpdatePrefix(tableName,entity,providerVO.getIgnoreNull()) + " WHERE id = #{id}";
        Console.info("updateById", tableName,sql,providerVO.getId());
        return sql;
    }

    public static <T> String updateByIds(ProviderVO<T> providerVO) {
        String tableName = providerVO.getTableName();
        T entity = providerVO.getEntity();
        String sql =
                "<script>"
                        + getUpdatePrefix(tableName,entity,providerVO.getIgnoreNull())
                        + " WHERE id IN "
                        + "<foreach collection='idList' item='item' index='index' open='(' separator=',' close=')'>"
                        + " #{item}"
                        + "</foreach>"
                        + "</script>";
        Console.info("updateByIds", tableName, sql, providerVO.getIdList());
        return sql;
    }

//    public static <T,K extends BaseQuery<T>> String updateByQuery(K query) {
//        String tableName = query.getTableName();
//        T entity = query.getEntityInstance();
//        String sql = "<script>"
//                + getUpdatePrefix(tableName,entity,true)
//                + SqlFieldReader.getDynamicTableCondition(query)
//                + "</script>";
//        Console.info("selectCount", tableName , sql, query);
//        return sql;
//    }

    /**
     * 获取更新操作的前缀部分
     * @param entity    实体对象
     * @param ignoreNull   更新时是否忽略空值
     * @return UPDATE 表名 SET
     */
    private static <T> String getUpdatePrefix(String tableName,T entity,boolean ignoreNull){
        StringBuilder builder = new StringBuilder();
        builder.append("UPDATE ").append(tableName).append(" SET ");
        List<String> fields = SqlFieldReader.getFields(entity);
        try{
            for(String field:fields){
                if(!ignoreNull || ReflectUtils.hasValue(entity,field)){
                    builder.append(StringUtils.humpToLine(field))
                            .append(" = #{entity.")
                            .append(field)
                            .append("} ")
                            .append(",");
                }
            }
            return builder.substring(0,builder.lastIndexOf(",")) ;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
